package com.kahriboo.log4mon.appender;

import java.io.OutputStream;
import java.net.Socket;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;

import com.kahriboo.log4mon.data.LogEventProtos.LogEvent;

/**
 * The Class Log4MonAppender.
 */
public class Log4MonAppender extends AppenderSkeleton {

	/** The host. */
	private String host = "localhost";

	/** The port. */
	private int port = 3105;

	/** The stream. */
	private OutputStream stream;

	/**
	 * The Constructor.
	 */
	@Override
	public void activateOptions() {
		try {
			System.out.println("Host = " + getHost());
			System.out.println("Port = " + getPort());
			final Socket socket = new Socket(getHost(), getPort());
			stream = socket.getOutputStream();
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void append(final LoggingEvent event) {
		final LogEvent logevent = LogEventFactory.createFrom(event);

		try {
			logevent.writeDelimitedTo(stream);
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	public void close() {
	}

	public boolean requiresLayout() {
		return false;
	}

	/**
	 * Gets the host.
	 * 
	 * @return the host
	 */
	public String getHost() {
		return host;
	}

	/**
	 * Sets the host.
	 * 
	 * @param host the host
	 */
	public void setHost(final String host) {
		this.host = host;
	}

	/**
	 * Gets the port.
	 * 
	 * @return the port
	 */
	public int getPort() {
		return port;
	}

	/**
	 * Sets the port.
	 * 
	 * @param port the port
	 */
	public void setPort(final int port) {
		this.port = port;
	}
}
